#-*-coding:utf-8-*-
'''
Created on 2011-1-16

@author: zhaoyh
@version: 0.1
'''

import web
from admin.config import view
from db.auth import User,Group
from core.meta import PageList,DBSession,makewhere
import admin.models.user as m_user
from utils.utility import  hash_password,AppStatus
class resource():
    '''
         请求页面时使用get，提交数据时使用post
    '''
    pass


class list():
    '''
         请求页面时使用get，提交数据时使用post
    '''
    def GET(self,name):
        page=web.input(numPerPage=20,pageNum=1,keywords='',orderField='user_id',col='')
        #titles={'group_code':'用户组代码','group_name':'用户组名称','note':'备注'}    
        titles=web.Storage({'user_id':'用户名','user_name':'姓名','user_email':'email','is_lock':'启用'})
        where=makewhere(page.keywords,page.col,titles)        
        list=PageList(User,page,where)
        return view.user_list(list=list.list(),page=list.Pageinfo(),title=titles)
    def POST(self,name):
        return self.GET(name)    
    
class add():
    def GET(self):
        group=DBSession.query(Group).order_by(Group.group_code)
        #g=group.getQuery('group_code', limit, offset, col, keyword)
        return view.user_add(g=group)
    def POST(self):
        u=web.input(user_id=None,add_next=None)
        if u.user_id==None:return self.GET()
        m=m_user.UserModel()
        s=m.check_code_only(u.user_id)
        status=AppStatus()
        if s:
            status.statusCode='error'
            status.message='代码重复，请修改后保存'
        else:
           
            s=m.add(user_id=u.user_id,user_name=u.user_name,
                    user_pwd=hash_password(u.user_pwd),user_email=u.user_email, 
                    is_lock=u.is_lock,group_id=u.group_id)
            if s:
                status.statusCode='ok'
                status.message='添加成功'
                status.navtabId="user_list"
                status.navtaburl="/admin/user/"
            else:
                status.statusCode='error'
                status.message='添加失败，请与管理员联系'  
        print  status.status()             
        return status.status()
            
class edit():
    def GET(self,user_id):
        edit_user=DBSession.query(User).filter(User.user_id==user_id).first()       
        group=DBSession.query(Group).order_by(Group.group_code)
        if edit_user==None:
            return  '{"statusCode":"300", "message":"数据已删除请刷新后重试"}'
        else:
            return view.user_edit(user=edit_user,g=group)
    def POST(self,name):
        status=AppStatus()            
        u=web.input(is_lock='N',user_pwd=None)
        e=m_user.UserModel()
        edit={}
        if u.user_pwd<>None or u.user_pwd<>u'':edit['user_pwd']=hash_password(u.user_pwd)
        edit['user_name']=u.user_name
        edit['user_email']=u.user_email
        edit['is_lock']=u.is_lock
        if e.edit(u.user_id,**edit):
            status.statusCode='ok'
            status.message='编辑成功'
            status.navtabId="user_list"
            status.navtaburl="/admin/user/"
                
        else:
            status.statusCode='error'
            status.message='错误，请与管理员联系'
            return '{"statusCode":"300", "message":"错误，请与管理员联系"}'    
        return status.status()         
        
class delete():

    def POST(self,group_id):
        u=web.input(ids=[])
        print u
        error_list=[]
        ok_list=[]
        for id in u.ids:
            e=m_user.UserModel()
            if e.delete(id):
                ok_list.append(id)
            else:
                error_list.append(id)
        if error_list:
            return '{"statusCode":"300", "message":"%s删除错误，请与管理员联系"}' %error_list
        else:
            return '{"statusCode":"200", "message":"删除成功"}'

